/* NoX (NoC Simulator)
 *
 * Dept. of Computer Science & Engineering, Pennsylvania State University.
 * All Rights Reserved.
 *  
 * 1. License     
 * NoX is distributed free of charge for academic, educational, noncommercial 
 * research purposes as long as this notice in its entirety is preserved in
 * every file included in this package.
 * All commercial use of this program requires separate licence. Contact the
 * author for details.
 * 
 * 2. All the publications that used the simulation results generated by the 
 * NoX should notify the author of the publication information and put 
 * following reference.
 *
 *  http://www.cse.psu.edu/~dpark/nox/
 * 
 * 3. Modification of the source code is permitted and encouraged as long as 
 * it follows the terms described in this copyright notice.
 *
 * 4. The author is not responsible for any problems caused by possible errors
 * of the NoX package. Therefore, users should verify the simulation result
 * before using it in their publication.
 *
 * Dept. of Computer Science & Engineering, Pennsylvania State University.
 * Contact: dpark@cse.psu.edu 
 * 
 * 6. If problems are found with the NoX package, please send an email to the
 * author for discussion and correction.

 */

/* Update History
 *
 * Jan. 31, 2006  Version 1.0 released by Dongkook Park 
 *
 */


#ifndef FLIT_H
#define FLIT_H

#define FLIT_POOL_SZ 200000L

#define HEAD 0x1
#define MIDL 0x2
#define TAIL 0x4


#define HEAD_FLIT (flit_ptr->flit_type & ((unsigned int)HEAD))
#define TAIL_FLIT (flit_ptr->flit_type & ((unsigned int)TAIL))
#define MIDL_FLIT (flit_ptr->flit_type & ((unsigned int)MIDL))

#define IS_HEAD_FLIT(ptr) ((ptr)->flit_type & ((unsigned int)HEAD))
#define IS_TAIL_FLIT(ptr) ((ptr)->flit_type & ((unsigned int)TAIL))
#define IS_MIDL_FLIT(ptr) ((ptr)->flit_type & ((unsigned int)MIDL))


// Flit types
/*
typedef enum {
  HEAD = 0x1,     
  MIDL = 0x2,
  TAIL = 0x4
} flit_typ_t;
*/

typedef enum { CONTROL , DATA  } msg_type_t;

/* We should make the following union, but later */
typedef struct {
  int snode;      
  int dnode; 	  
  int ejt_pc; 
  int inj_pc;
  int dest;
  int src;
} flit_data_t;

typedef struct flit_struct{
  //flit_typ_t flit_type;
  unsigned int flit_type;
  flit_data_t data;
  long long app_inj_tm;   // message generation time (injection to system(l1miss))
#ifdef TR_INTEG
  long long inj_tm;       // flit injection time to the network (router)
  long long msg_inj_tm;   // message generation time (injection to NIC)
#else
  unsigned int inj_tm;       // flit injection time to the network (router)
  unsigned int msg_inj_tm;   // message generation time (injection to NIC)
#endif
  int vc_num;                // vc that this flit is using
  struct flit_struct *next_free;
  int flit_num;              // flit number
  long long entry_time;            // the simulation time this flit enters a router module. Used when calculating the network delay.
  int delay_cycle;           // cumulative delay cycle spent in the queues waiting for its turn.
  int interference_cycles;
  int buff_full_interference_cycles;
  int priority_inversion_cycles;
  int sa_cycles;
  int bus_latency;           // cumulative delay cycle spent in the queues waiting for its turn.
  int num_hop;		     // number of hops this flit passed
  int err_chk_cnt;
  int error;
  int error_penalty;         // penalty cycles in case of soft error.
  int e2e_penalty;           // penalty for end-to-end retransmission.
  int pos;                   // flit position in the message.(0 ~ MSG_LEN-1) 0:HEAD, MSG_LEN-1:TAIL

  unsigned blk_chk_done       : 1; // indicate whether this flit should be blocked or not (due to soft error)
  unsigned is_nack            : 1; // Indicates whether this flit is a NACK flit. Used in E2E retransmission
  unsigned is_sx_less_than_dx : 1; // indicate whether x-coordinate of the source node is less than that of the destination node.
  unsigned is_sy_less_than_dy : 1; // indicate whether y-coordinate of the source node is less than that of the destination node.

  int priority_id;
  int marking_weight;
  long long batch_id;

  float slack;
  unsigned int packet_id;

  int msglen ;
	int llen ;
  msg_type_t msgtype;
  short priority;
#ifdef TR_INTEG
  long long trid;  // PARENT TRANSACTION;
#endif
  int short_flit;
} flit_t;

flit_t* flit_alloc(void);          // Allocate memory for flit structure
void flit_free(flit_t *flit_ptr);  // Deallocate memory used for the flit structure
void init_free_list(void);
void make_body_flit(flit_t *flit_ptr, flit_t *src_ptr);  // Deallocate memory used for the flit structure

#endif
